package com.qs.rocketmq.common.util;

import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class ErrorMsgCacheUtil {

    private static Map<String, String> cache = new HashMap<>();

    public static void init() {
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource resource = resolver.getResource("/props/error.properties");
        try {
            Properties props = PropertiesLoaderUtils.loadProperties(new EncodedResource(resource, "UTF-8"));
            Enumeration<?> en = props.propertyNames();
            while (en.hasMoreElements()) {
                String errCode = (String) en.nextElement();
                String errMsg = props.getProperty(errCode);
                cache.put(errCode, errMsg);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getErrorMsg(long errCode) {
        return cache.get(String.valueOf(errCode));
    }
}
